#include "OSMWay.h"
#include <iostream>

OSMWay::OSMWay()
{

}

unsigned int OSMWay::getId()
{
    return m_id;
}

void OSMWay::setId(unsigned int id)
{
    m_id = id;
}

const QMap<QString, QString>* OSMWay::getFeatures()
{
    return &m_features;
}

const QVector<unsigned int>* OSMWay::getNodeIds()
{
    return &m_nodeIds;
}

void OSMWay::addNodeId(unsigned int nodeId)
{
    m_nodeIds.push_back(nodeId);
}

void OSMWay::addFeature(const QString& key, const QString& value)
{
    m_features.insert(key, value);
}

bool OSMWay::isWaterArea(QString& category, QString& name)
{
    QString naturalKey("natural");
    QString waterwayKey("waterway");
    QString nameKey("name");

    if(m_features.contains(naturalKey))
    {
        if(m_features.value(naturalKey) != "water")
        {
            return false;
        }

        category = m_features.value(naturalKey);
        
        if(m_features.contains(nameKey))
        {
            name = m_features.value(nameKey);
        }
        else
        {
            name = "none";
        }

        return true;
    }

    if(m_features.contains(waterwayKey))
    {
        if(m_features.value(waterwayKey) == "river" || // is line
            m_features.value(waterwayKey) == "stream" || // is line
            m_features.value(waterwayKey) == "canal" || // is line
            m_features.value(waterwayKey) == "drain" || // is line
            m_features.value(waterwayKey) == "ditch") // is line
        {
            category = m_features.value(waterwayKey);

            if(m_features.contains(nameKey))
            {
                name = m_features.value(nameKey);
            }
            else
            {
                name = "none";
            }

            return true;
        }
    }

    return false;
}

bool OSMWay::isStreet(QString& category, QString& name)
{
    QString streetKey("highway");
    QString nameKey("name");

    if(!m_features.contains(streetKey))
    {
        return false;
    }

    category = m_features.value(streetKey);

    if(m_features.contains(nameKey))
    {
        name = m_features.value(nameKey);
    }
    else
    {
        name = "none";
    }

    return true;
}

bool OSMWay::isResidential(QString& category, QString& name)
{
    QString keyResidential("residential");
    if(m_features.contains("landuse"))
    {
        if(m_features.value("landuse") != keyResidential)
        {
            return false;
        }

        category = m_features.value("landuse");

        if(m_features.contains("name"))
        {
            name = m_features.value("name");
        }
        else
        {
            name = "none";
        }

        return true;
    }

    return false;
}

bool OSMWay::isGreenArea(QString& category, QString& name)
{
    QString woodKey("wood");
    QString scrubKey("scrub");

    QString grassKey("grass");
    QString forestKey("forest");

    if(m_features.contains("landuse"))
    {
        if(m_features.value("landuse") != grassKey && m_features.value("landuse") != forestKey)
        {
            return false;
        }

        category = m_features.value("landuse");

        if(m_features.contains("name"))
        {
            name = m_features.value("name");
        }
        else
        {
            name = "none";
        }

        return true;
    }

    if(m_features.contains("natural"))
    {
        if(m_features.value("natural") != woodKey && m_features.value("natural") != scrubKey)
        {
            return false;
        }

        category = m_features.value("natural");

        if(m_features.contains("name"))
        {
            name = m_features.value("name");
        }
        else
        {
            name = "none";
        }

        return true;    
    }

    return false;
}

bool OSMWay::isBuilding(QString& name, QString& addrCity, QString& addrCountry, QString& addrPostcode, QString& addrStreet, QString& addrSuburb)
{
    QString buildingKey("building");
    QString nameKey("name");
    QString cityKey("addr:city");
    QString countryKey("addr:country");
    QString postcodeKey("addr:postcode");
    QString streetKey("addr:street");
    QString suburbKey("addr:suburb");

    if(!m_features.contains(buildingKey))
    {
        return false;
    }

    if(m_features.contains(nameKey))
    {
        name = m_features.value(nameKey);
    }

    if(m_features.contains(cityKey))
    {
        addrCity = m_features.value(cityKey);
    }
    else
    {
        addrCity = "none";
    }

    if(m_features.contains(cityKey))
    {
        addrCity = m_features.value(cityKey);
    }
    else
    {
        addrCity = "none";
    }

    if(m_features.contains(countryKey))
    {
        addrCountry = m_features.value(countryKey);
    }
    else
    {
        addrCountry = "none";
    }

    if(m_features.contains(postcodeKey))
    {
        addrPostcode = m_features.value(postcodeKey);
    }
    else
    {
        addrPostcode = "none";
    }

    if(m_features.contains(streetKey))
    {
        addrStreet = m_features.value(streetKey);
    }
    else
    {
        addrStreet = "none";
    }

    if(m_features.contains(suburbKey))
    {
        addrSuburb = m_features.value(suburbKey);
    }
    else
    {
        addrSuburb = "none";
    }

    return true;
}